home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / 3DDEMO.ZIP / 3D / SOURCE / 3DDEMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-22  |  12.1 KB  |  454 lines

  1. #include <stdlib.h>
  2. #include <stdarg.h>
  3. #include <string.h>
  4.  
  5. #include "defines.hpp"
  6. #include "input.hpp"
  7. #include "video.hpp"
  8. #include "voxel.hpp"
  9. #include "mesh.hpp"
  10. #include "file.hpp"
  11. #include "bmapgfx.hpp"
  12.  
  13. // Copyright (c) 1996 by Kerrigan Burgess, all rights reserved.
  14.  
  15. VIDEOCLASS        *Video;
  16. VOXELCLASS        *Voxel;
  17. MESHCLASS         *Mesh;
  18. FILESYSTEMCLASS   *File;
  19. BITMAPGFXCLASS    *Bitmap;
  20. INPUTCLASS        *Input;
  21.  
  22. void Error(char *fmt, ...);        // Always put.
  23.  
  24. void Build(const int Device, int Mode);
  25. void TearDown(void);
  26. void Demo1(void);
  27. void Demo2(void);
  28. void Demo3(void);
  29. void Handle3DInput(void);
  30. void ShowMesh(int shade,int mode);
  31. void ShowMeshEffects(int effect);
  32.  
  33.  
  34. void main(int argc, char **argv)
  35. {
  36.  
  37.    cout<<"\n\n\n\n\n ROTATION MOVEMENTS:\n";
  38.    cout<<"  +X    --> < INSERT >     +Y --> < HOME >      +Z --> < PGUP >\n";
  39.    cout<<"  -X    --> < DELETE >     -Y --> < END >       -Z --> < PGDN >\n\n";
  40.    cout<<"                     ALL STOP: < F1 >\n\n";
  41.    cout<<"  CAMERA MOVEMENTS:\n";
  42.    cout<<"  < LEFT ARROW > < RIGHT ARROW > < UP ARROW > < DOWN ARROW >\n\n\n\n\n";
  43.    
  44.    cout<<"   Please press < esc >  >>> EACH <<< time in order to continue demo\n\n";
  45.    while (!kbhit());
  46.    
  47.    Build(KEYBOARD,MCGA_320_200_256);
  48.  
  49.      Demo1();
  50.      Demo2();
  51.      Demo3();
  52.  
  53.    TearDown();
  54.  
  55.    cout<<"\n\n              Hope you liked my Demo\n\n";
  56.     
  57. }
  58.  
  59. void Error(char *fmt, ...)   
  60. {
  61.   char msg[80];
  62.   va_list argptr;
  63.  
  64.   va_start(argptr, fmt);
  65.   vsprintf(msg,fmt,argptr);
  66.   va_end(argptr);
  67.   TearDown();
  68.   cout<<msg;
  69.   exit(1);
  70. }
  71.  
  72. void Build(const int Device, int Mode)
  73. {
  74.     Video  = NULL;
  75.     Voxel  = NULL;
  76.     Mesh   = NULL;
  77.     File   = NULL;
  78.     Bitmap = NULL;
  79.     Input  = NULL;
  80.  
  81.     Video        = new VIDEOCLASS;
  82.     if (Video==NULL)
  83.       Error("Not enough memory\n");      
  84.  
  85.     switch (Device)
  86.      {
  87.        case KEYBOARD:
  88.          Input = new KEYBOARDCLASS;
  89.          if (Input==NULL)
  90.            Error("Not enough memory\n");
  91.        break;
  92.        case JOYSTICK:
  93.        break;
  94.        case MOUSE:
  95.        break;
  96.        default:
  97.        break;
  98.      }
  99.  
  100.     Video->InitVideo(Mode);                   // initialize video mode.
  101.     Video->CreateBuffer(0);                   // create double buffer, flood with black color.
  102. }
  103.  
  104. void TearDown(void)
  105. {
  106.     if (Input != NULL)
  107.       delete Input;   
  108.     if (Video != NULL)
  109.       delete Video;
  110.     if (Voxel != NULL)
  111.       delete Voxel;      
  112.     if (Bitmap != NULL)
  113.       delete Bitmap;
  114.     if (Mesh != NULL)
  115.       delete Mesh;
  116.     if (File != NULL)
  117.       delete File;
  118. }
  119.  
  120. void Demo1(void)
  121. {
  122.     
  123.     Video->LoadBackground("images/intro.pcx");
  124.     Video->SetAllRgbPalette();
  125.  
  126.     Video->FadeToColor();
  127.       WaitFor(20);
  128.     Video->FadeToBlack();
  129.     Video->DeleteBackground();          // don't need background any more.
  130.  
  131.     Bitmap     = new BITMAPGFXCLASS;
  132.       if (Bitmap==NULL)
  133.         Error("Not enough memory\n");
  134.   
  135.     Bitmap->Create( Video->GetStats(), Video->LoadPcxFile("images/biglake.pcx") );
  136.     Video->SetAllRgbPalette();
  137.  
  138.     float scale=0.05;
  139.  
  140.     while ( (!Input->Is_Key(SCAN_ESC)) && (scale < 1.95) )             // SCALING OF BITMAPS
  141.     {
  142.        Video->ClearBuffer();
  143.          scale+=0.05;
  144.          Bitmap->Scale(Video->GetBuffer(),scale);
  145.        Video->Buf2Video();
  146.     }
  147.  
  148.     WaitFor(15);
  149.          
  150.     while ( (!Input->Is_Key(SCAN_ESC)) && (scale > 0.05) )
  151.     {
  152.        Video->ClearBuffer();
  153.          scale-=0.05;
  154.          Bitmap->Scale(Video->GetBuffer(),scale);
  155.        Video->Buf2Video();
  156.     }
  157.  
  158.     Input->ClearKeys();      // clear all pending key presses.
  159.     
  160.     while ( (!Input->Is_Key(SCAN_ESC)) && (scale < 1.95) )              // ROTATION OF BITMAPS
  161.     {
  162.        Video->ClearBuffer();
  163.          scale+=0.04;
  164.          Bitmap->Rotate(Video->GetBuffer(),3,scale);
  165.        Video->Buf2Video();
  166.     }
  167.  
  168.     while ( (!Input->Is_Key(SCAN_ESC)) && (scale > 0.05) )
  169.     {
  170.        Video->ClearBuffer();
  171.          scale-=0.04;
  172.          Bitmap->Rotate(Video->GetBuffer(),3,scale);
  173.        Video->Buf2Video();
  174.     }
  175.  
  176.     while ( (!Input->Is_Key(SCAN_ESC)) && (scale < 0.95) )   
  177.     {
  178.        Video->ClearBuffer();
  179.          scale+=0.04;
  180.          Bitmap->Rotate(Video->GetBuffer(),3,scale);
  181.        Video->Buf2Video();
  182.     }
  183.  
  184.    WaitFor(15);                  // let viewer admire scenery. (Yah right!)
  185.    Video->FadeToBlack();
  186. }
  187.  
  188. void Demo2(void)
  189. {
  190.  
  191.     Video->LoadBackground("images/3dtrans.pcx");
  192.     Video->SetAllRgbPalette();
  193.  
  194.     Video->FadeToColor();
  195.       WaitFor(20);
  196.     Video->FadeToBlack();
  197.     Video->DeleteBackground();          // don't need background any more.
  198.  
  199.     Mesh       = new MESHCLASS;
  200.       if (Mesh==NULL)
  201.         Error("Not enough memory\n");      
  202.  
  203.     File       = new FILESYSTEMCLASS;
  204.       if (File==NULL)
  205.         Error("Not enough memory\n");      
  206.  
  207.     File->CreateDataBase(Mesh,"meshes/duck.asc",PARENT);
  208.     File->CreateDataBase(Mesh,"meshes/sphere1.asc",PARENT);
  209.     File->CreateDataBase(Mesh,"meshes/sphere2.asc",CHILD);
  210.     File->CreateDataBase(Mesh,"meshes/sphere3.asc",CHILD);
  211.  
  212.     Mesh->CreateMeshList();                   // List to hold all polygons.
  213.     Mesh->SetMaxTransparency( Video->LoadTransTable("data/trans.tbl") );       // load in transparency table.
  214.  
  215.     Video->LoadPalTable("data/standard.pal");          // load predefined linear palette.
  216.     Video->LoadPalPhongTable("data/phong.pal");     // load predefined phong palette.
  217.     Video->LoadPhongTBL("data/phong.tbl");             // load precomputed angle table. Cos*NUM_SHADES.
  218.     Video->LoadHazeTable("data/haze.tbl");
  219.  
  220.     Mesh->SetTSRVectors(12,8,5,0,0,0);        // start object rotating
  221.     Input->ClearKeys();            // reset all pending key presses
  222.  
  223.     ShowMesh(WireFrame,NoTexture);
  224.     ShowMesh(Lambert,NoTexture);
  225.     ShowMesh(Gouraud,NoTexture);
  226.     ShowMesh(Phong,NoTexture);
  227.     ShowMesh(Lambert,Texture);
  228.     ShowMesh(Gouraud,Texture);
  229.     ShowMesh(Phong,Texture);
  230.  
  231.     Input->ClearKeys();            // reset all pending key presses
  232.  
  233.     ShowMeshEffects(Shadow);
  234.     ShowMeshEffects(Morph);
  235.     ShowMeshEffects(Transparent);
  236.     ShowMeshEffects(Haze);
  237.  
  238.     Video->FadeToBlack();
  239. }
  240.  
  241. void Demo3(void)
  242. {
  243.     
  244.     Video->LoadBackground("images/voxelgr.pcx");
  245.     Video->SetAllRgbPalette();
  246.  
  247.     Video->FadeToColor();
  248.       WaitFor(10);
  249.     Video->FadeToBlack();
  250.     Video->DeleteBackground();          // don't need background any more.
  251.  
  252.  
  253.     Voxel        = new VOXELCLASS;
  254.     if (Voxel==NULL)
  255.       Error("Not enough memory\n");      
  256.  
  257.     Voxel->CreateDataBase(Video);
  258.  
  259.     Input->ClearKeys();            // reset all pending key presses
  260.     while (!Input->Is_Key(SCAN_ESC))
  261.     {
  262.        Video->ClearBuffer();
  263.           Voxel->HandleInput(Input);
  264.           Voxel->RenderView( Video->GetBuffer() );
  265.        Video->Buf2Video();
  266.     }
  267.  
  268.   Video->FadeToBlack();
  269. }
  270.  
  271. void Handle3DInput(void)
  272. {
  273.    int rotangle_x=0,rotangle_y=0,rotangle_z=0;
  274.    float viewx=0,viewy=0,viewz=0;
  275.  
  276.    if (Input->Is_Key(SCAN_ANY))            // first see if any key is pressed!
  277.    {
  278.      if (Input->Is_Key(SCAN_F1))           // all stop!
  279.        Mesh->ResetTSRVectors();
  280.  
  281.      if (Input->Is_Key(SCAN_INSERT))       // +X
  282.        rotangle_x=1;
  283.      else
  284.      if (Input->Is_Key(SCAN_DELETE))       // -X
  285.        rotangle_x=-1;
  286.  
  287.      if (Input->Is_Key(SCAN_HOME))         // -Y
  288.        rotangle_y=-1;
  289.      else
  290.      if (Input->Is_Key(SCAN_END))          // +Y
  291.        rotangle_y=1;
  292.     
  293.      if (Input->Is_Key(SCAN_PGUP))         // +Z
  294.        rotangle_z=1;
  295.      else
  296.      if (Input->Is_Key(SCAN_PGDOWN))       // -Z
  297.        rotangle_z=-1;
  298.     
  299.     if (Input->Is_Key(SCAN_F))
  300.        viewz=25;
  301.      else
  302.      if (Input->Is_Key(SCAN_B))
  303.        viewz=-25;
  304.  
  305.     Mesh->SetTSRVectors(rotangle_x,rotangle_y,rotangle_z,0,0,0);
  306.     Mesh->MoveCamera(0,0,0,viewx,viewy,viewz);
  307.  
  308.     if (Input->Is_Key(SCAN_MINUS))
  309.       Mesh->SetTransparencyLevel(-1);
  310.     else
  311.     if (Input->Is_Key(SCAN_EQUAL))
  312.       Mesh->SetTransparencyLevel(1);
  313.    }
  314. }
  315.  
  316. void ShowMesh(int shade,int mode)
  317. {
  318.  
  319.  Input->ClearKeys();            // reset all pending key presses
  320.  
  321.   switch ( shade | mode )
  322.   {
  323.     case WireFrame:
  324.       Video->LoadBackground("images/sunwr.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  325.     break;
  326.     case Lambert:
  327.       Video->LoadBackground("images/sunlb.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  328.     break;
  329.     case Gouraud:
  330.       Video->LoadBackground("images/sungr.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  331.     break;
  332.     case Phong:
  333.       Video->LoadBackground("images/sunpg.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  334.     break;
  335.     case Lambert_Texture:
  336.       Video->LoadBackground("images/sunlbtx.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  337.     break;
  338.     case Gouraud_Texture:
  339.       Video->LoadBackground("images/sungrtx.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  340.     break;
  341.     case Phong_Texture:
  342.       Video->LoadBackground("images/sunpgtx.pcx");   // creates a double buffer, size depending on mode. Allocates space for MeshList.
  343.     break;
  344.     default:
  345.     break;
  346.   }
  347.         
  348.     Mesh->SetShading( shade );
  349.     Mesh->SetTextureMapping( mode );
  350.     
  351.     while (!Input->Is_Key(SCAN_ESC))
  352.     {
  353.         Video->ClearBuffer();
  354.           Handle3DInput();
  355.           Mesh->_3DPipeLine();         // do the rendering pipeline.
  356.         Video->Buf2Video();
  357.     }
  358.   Input->ClearKeys();            // reset all pending key presses
  359. }
  360.  
  361. void ShowMeshEffects(int effect)
  362. {
  363.  
  364.  Input->ClearKeys();            // reset all pending key presses
  365.  
  366.   switch (effect)
  367.   {
  368.      case Shadow:
  369.      
  370.        Video->LoadBackground("images/sunshd.pcx"); 
  371.  
  372.        Mesh->SetShadow( Shadow );                // enable effects.
  373.        Mesh->SetShading( Phong );
  374.        Mesh->SetTextureMapping( NoTexture );
  375.        
  376.        Mesh->Light[0].SetLight(-100,0,-1);     // move light to better see shadow.
  377.               
  378.        while (!Input->Is_Key(SCAN_ESC))
  379.        {
  380.           Video->ClearBuffer();   
  381.             Handle3DInput();
  382.             Mesh->_3DPipeLine();         // do the rendering pipeline.
  383.           Video->Buf2Video();
  384.        }
  385.  
  386.      Mesh->SetShadow( NoShadow );
  387.      Mesh->Light[0].SetLight(0,0,-1);       // reset light source.
  388.      break;
  389.  
  390.      case Morph:
  391.  
  392.        Video->LoadBackground("images/sunmph.pcx"); 
  393.  
  394.        Mesh->SetMorph( Morph );
  395.        Mesh->SetShading( Phong );
  396.        Mesh->SetTextureMapping( Texture );
  397.        
  398.        while (!Input->Is_Key(SCAN_ESC))
  399.        {
  400.           Video->ClearBuffer();
  401.             Handle3DInput();
  402.             Mesh->_3DPipeLine();         // do the rendering pipeline.
  403.           Video->Buf2Video();
  404.        }
  405.  
  406.        Mesh->SetMorph( NoMorph );   // shut off.
  407.      break;
  408.  
  409.      case Transparent:
  410.  
  411.        Video->LoadBackground("images/suntrn.pcx"); 
  412.  
  413.        Mesh->SetTransparency( Transparent );       
  414.        Mesh->SetShading( Phong );
  415.        Mesh->SetTextureMapping( Texture );
  416.  
  417.        while (!Input->Is_Key(SCAN_ESC))
  418.        {
  419.           Video->ClearBuffer();   
  420.             Handle3DInput();
  421.             Mesh->_3DPipeLine();         // do the rendering pipeline.
  422.           Video->Buf2Video();
  423.        }
  424.  
  425.      Mesh->SetTransparency( Opaque );
  426.      break;
  427.  
  428.      case Haze:
  429.  
  430.        Video->LoadBackground("images/sunhz.pcx");  
  431.  
  432.        Mesh->SetHazing( Haze );
  433.        Mesh->SetShading( Gouraud );
  434.        Mesh->SetTextureMapping( Texture );
  435.        Mesh->MoveCamera(0,0,0,0,0,-150);    // move object into furthur into haze ( easier to see it fogged ).
  436.  
  437.        while (!Input->Is_Key(SCAN_ESC))
  438.        {
  439.           Video->ClearBuffer();
  440.             Handle3DInput();
  441.             Mesh->_3DPipeLine();         // do the rendering pipeline.
  442.           Video->Buf2Video();
  443.        }
  444.  
  445.        Mesh->SetHazing( NoHaze );
  446.      break;
  447.  
  448.      default:
  449.      break;
  450.   }
  451.  Input->ClearKeys();            // reset all pending key presses
  452. }
  453.  
  454.